package com.whfc.common.util;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import java.util.Collection;

/**
 * 断言工具类，用于简化条件判断和异常抛出
 */
public class Assert {

    /**
     * 断言对象不为null，否则抛出参数错误异常
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new BizException(ResultEnum.PARAM_ERROR, message);
        }
    }

    /**
     * 断言对象不为null，否则抛出指定结果枚举的异常
     */
    public static void notNull(Object object, ResultEnum resultEnum) {
        if (object == null) {
            throw new BizException(resultEnum);
        }
    }

    /**
     * 断言表达式为true，否则抛出参数错误异常
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new BizException(ResultEnum.PARAM_ERROR, message);
        }
    }

    /**
     * 断言表达式为true，否则抛出指定结果枚举的异常
     */
    public static void isTrue(boolean expression, ResultEnum resultEnum) {
        if (!expression) {
            throw new BizException(resultEnum);
        }
    }

    /**
     * 断言字符串不为空，否则抛出参数错误异常
     */
    public static void notEmpty(String str, String message) {
        if (str == null || str.trim().isEmpty()) {
            throw new BizException(ResultEnum.PARAM_ERROR, message);
        }
    }

    /**
     * 断言集合不为空，否则抛出参数错误异常
     */
    public static void notEmpty(Collection<?> collection, String message) {
        if (collection == null || collection.isEmpty()) {
            throw new BizException(ResultEnum.PARAM_ERROR, message);
        }
    }

    /**
     * 断言条件成立，否则抛出业务异常
     */
    public static void check(boolean condition, String code, String message) {
        if (!condition) {
            throw new BizException(code, message);
        }
    }
}